package org.study.id.baidu.config;

import com.xfvape.uid.UidGenerator;
import com.xfvape.uid.impl.CachedUidGenerator;
import com.xfvape.uid.impl.DefaultUidGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 将 uid-generator 核心对象装配为 spring 的 bean。
 * uid-generator 提供了两种生成器：DefaultUidGenerator、CachedUidGenerator。
 * 如对 UID 生成性能有要求，请使用 CachedUidGenerator。
 *
 * @author Administrator
 * @date 2021-05-27
 */
@Configuration
public class WorkerNodeConfig {
    @Bean("disposableWorkerIdAssigner")
    public DisposableWorkerIdAssigner disposableWorkerIdAssigner() {
        // 注意使用我們自定义的 org.study.mybatis.boot.config.DisposableWorkerIdAssigner
        // 而不能使用 com.xfvape.uid.worker.DisposableWorkerIdAssigner
        DisposableWorkerIdAssigner disposableWorkerIdAssigner = new DisposableWorkerIdAssigner();
        return disposableWorkerIdAssigner;
    }

    @Bean("defaultUidGenerator")
    public UidGenerator defaultUidGenerator(DisposableWorkerIdAssigner disposableWorkerIdAssigner) {
        DefaultUidGenerator defaultUidGenerator = new DefaultUidGenerator();
        defaultUidGenerator.setWorkerIdAssigner(disposableWorkerIdAssigner);
        return defaultUidGenerator;
    }

    @Bean("cachedUidGenerator")
    public UidGenerator cachedUidGenerator(DisposableWorkerIdAssigner disposableWorkerIdAssigner) {
        CachedUidGenerator cachedUidGenerator = new CachedUidGenerator();
        cachedUidGenerator.setWorkerIdAssigner(disposableWorkerIdAssigner);
        return cachedUidGenerator;
    }


}
